#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
#
#  filemanager.py
#
#  Copyright 2011 Arne Hüffmeier <ahueffme@gmail.com>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
#

import os
import sys


def getDirList(path):
	return os.listdir(path)


def getFileList(path):
	#print(os.pwd())
	fList = []
	for root, dirs, files in os.walk(path):
		for file in files:
			fList.append((root, file))
	return fList


def setFiles(path, list):
	#print(path)
	for i in list:
		oldName = i[0][i[0].find("/") + 1:]
		newName = i[1].filename() + oldName[oldName.rfind("."):]
		pathComplet = path + "/" + i[0][:i[0].find("/") + 1]
		os.rename(os.path.join(pathComplet, oldName),
		os.path.join(pathComplet, newName))
	#os.utime(name, )


def getSerienID(path):
	try:
		serienFile = open(path + "/.id.txt")
	except FileNotFoundError:
		return None
	else:
		temp = serienFile.readline()
		serienFile.close()
		return temp


def setSerienID(path, serienId):
	try:
		serienFile = open(path + "/.id.txt", 'x')
		serienFile.write(serienId)
		serienFile.close()
	except FileExistsError:
		print("Die id.txt Datei in ", path, "existiert schon.")
	except:
		print("Unbekannter Fehler:", sys.exc_info()[0])
		raise

if __name__ == "__main__":
	#getFileList("./serien")
	#setFile("./serien/Castle/S01/S01E01.avi",
	# "./serien/Castle/S01/test.avi", "bla")
	print(getSerienID("./serien/Castle"))
	setSerienID("./serien/Castle", "12345")